import CryptoJS from 'crypto-js';

export class AESUtils {
    private static key: string = "4GT/xT88KVG0725G"; // 32字节密钥
    private static iv: string = "x/Hx3dZwl7800bZc";  // 16字节 IV

    // AES-256-CBC 加密
    public static encrypt(plainText: string): string {
        const key = CryptoJS.enc.Utf8.parse(AESUtils.key);
        const iv = CryptoJS.enc.Utf8.parse(AESUtils.iv);

        const encrypted = CryptoJS.AES.encrypt(plainText, key, {
            iv: iv,
            mode: CryptoJS.mode.CBC,
            padding: CryptoJS.pad.Pkcs7
        });

        return encrypted.toString();
    }

    // AES-256-CBC 解密
    public static decrypt(cipherText: string): string {
        const key = CryptoJS.enc.Utf8.parse(AESUtils.key);
        const iv = CryptoJS.enc.Utf8.parse(AESUtils.iv);

        const decrypted = CryptoJS.AES.decrypt(cipherText, key, {
            iv: iv,
            mode: CryptoJS.mode.CBC,
            padding: CryptoJS.pad.Pkcs7
        });

        return decrypted.toString(CryptoJS.enc.Utf8);
    }
}